package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.basic.enums.RespEnum;
import com.minimalist.common.entity.lindi.po.LindiPriceConfig;
import com.minimalist.common.entity.lindi.vo.priceconfig.LindiPriceConfigQueryVO;
import com.minimalist.common.entity.lindi.vo.priceconfig.LindiPriceConfigVO;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.mapper.LindiPriceConfigMapper;
import com.minimalist.lindi.service.LindiPriceConfigService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标交易平台_价格配置表 服务层实现。
 *
 * @author 11987
 * @since 2025-07-09
 */
@Service
public class LindiPriceConfigServiceImpl extends ServiceImpl<LindiPriceConfigMapper, LindiPriceConfig> implements LindiPriceConfigService{

    @Autowired
    private LindiPriceConfigMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPriceConfig(LindiPriceConfigVO priceConfigVO){
        LindiPriceConfig priceConfig = BeanUtil.copyProperties(
                priceConfigVO, LindiPriceConfig.class);
        //价格配置ID
        long priceConfigId = UnqIdUtil.uniqueId();
        priceConfig.setPriceConfigId(priceConfigId);
        //插入
        int insertCount = mapper.insert(priceConfig, true);
        Assert.isTrue(insertCount > 0
                , () -> new BusinessException(RespEnum.FAILED.getDesc()));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePriceConfigByPriceConfigId(Long priceConfigId){
        //查询价格配置
        LindiPriceConfig priceConfig = mapper.selectPriceConfigByPriceConfigId(priceConfigId);
        Assert.notNull(priceConfig, () -> new BusinessException("价格配置不存在"));
        //删除价格配置
        mapper.deletePriceConfigByPriceConfigId(priceConfigId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePriceConfigByPriceConfigId(LindiPriceConfigVO priceConfigVO){
        //查询价格配置
        LindiPriceConfig priceConfig = mapper.selectPriceConfigByPriceConfigId(
                priceConfigVO.getPriceConfigId());
        Assert.notNull(priceConfig, () -> new BusinessException("价格配置不存在"));
        LindiPriceConfig newPriceConfig = BeanUtil.copyProperties(priceConfigVO, LindiPriceConfig.class);
        //修改
        mapper.updatePriceConfigByPriceConfigId(newPriceConfig);
    }

    @Override
    public PageResp<LindiPriceConfigVO> getPagePriceConfigList(LindiPriceConfigQueryVO queryVO){
        //分页查询
        Page<LindiPriceConfig> priceConfigPage = mapper.selectPagePriceConfigList(queryVO);
        //数据转换
        List<LindiPriceConfigVO> priceConfigVOList = BeanUtil.copyToList(priceConfigPage.getRecords()
                , LindiPriceConfigVO.class);
        return new PageResp<>(priceConfigVOList, priceConfigPage.getTotalRow());
    }

    @Override
    public LindiPriceConfigVO getPriceConfigByPriceConfigId(Long priceConfigId){
        LindiPriceConfig priceConfig = mapper.selectPriceConfigByPriceConfigId(priceConfigId);
        Assert.notNull(priceConfig, () -> new BusinessException("价格配置不存在"));
        return BeanUtil.copyProperties(priceConfig, LindiPriceConfigVO.class);
    }

    @Override
    public LindiPriceConfig getValidPriceConfigByType(String priceType, java.time.LocalDateTime queryTime){
        LindiPriceConfig priceConfig = mapper.selectValidPriceConfigByType(priceType, queryTime);
        if (priceConfig == null){
            throw new BusinessException("未找到价格类型为 " + priceType + " 在时间 " + queryTime + " 的有效价格配置");
        }
        return priceConfig;
    }

}
